//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.games.CraftingEngine;

import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.io.TextColor;
import java.util.ArrayList;
import java.util.Iterator;

public class Element implements Comparable<Element> {
    private final String name;
    private ArrayList<Element.Recipe> allRecipes = new ArrayList();
    private final TextColor ConsoleColor;

    public Element(String name, String recipes, TextColor c) throws Exception {
        this.name = name;
        this.ConsoleColor = c;
        String[] recepieList = recipes.split(";");
        String[] var5 = recepieList;
        int var6 = recepieList.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String s = var5[var7];
            String[] ingredients = s.split("\\+");
            if (ingredients.length > 2) {
                throw new Exception("Illegal Crafting Recipe!");
            }

            if (ingredients.length > 1) {
                this.allRecipes.add(new Element.Recipe(ingredients[0], ingredients[1]));
            }
        }

    }

    public String GetName() {
        return this.name;
    }

    public TextColor GetColor() {
        return this.ConsoleColor;
    }

    public void DrawRecipes(IOHandler io) {
        io.SetTextColor(TextColor.WHITE);
        io.Write("Crafting recepie for ");
        io.SetTextColor(this.ConsoleColor);
        io.WriteLine(toTitleCase(this.name) + ":");
        io.SetTextColor(TextColor.WHITE);

        for(int i = 0; i < (new String("Crafting Recepies for " + this.name + ":")).length(); ++i) {
            io.Write("-");
        }

        io.WriteLine("");
        if (this.allRecipes.size() == 0) {
            io.SetTextColor(TextColor.MAGENTA);
            io.WriteLine(" *Uncraftable");
            io.SetTextColor(TextColor.WHITE);
        } else {
            Iterator var4 = this.allRecipes.iterator();

            while(var4.hasNext()) {
                Element.Recipe r = (Element.Recipe)var4.next();
                r.DrawRecipe(io);
            }
        }

        io.SetTextColor(TextColor.WHITE);
        io.WriteLine("");
    }

    public boolean TestElementCraftable(String e1, String e2) {
        Iterator var3 = this.allRecipes.iterator();

        Element.Recipe r;
        do {
            if (!var3.hasNext()) {
                return false;
            }

            r = (Element.Recipe)var3.next();
        } while(!r.TestRecipe(e1, e2));

        return true;
    }

    public void DrawElement(IOHandler io, boolean newline) {
        TextColor TempColor = io.GetTextColor();
        io.SetTextColor(this.ConsoleColor);
        if (newline) {
            io.WriteLine(toTitleCase(this.name));
        } else {
            io.Write(toTitleCase(this.name));
        }

        io.SetTextColor(TempColor);
    }

    public void DrawElement(IOHandler io) {
        this.DrawElement(io, true);
    }

    private static String toTitleCase(String input) {
        StringBuilder titleCase = new StringBuilder();
        boolean nextTitleCase = true;
        char[] var3 = input.toCharArray();
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            char c = var3[var5];
            if (Character.isSpaceChar(c)) {
                nextTitleCase = true;
            } else if (nextTitleCase) {
                c = Character.toTitleCase(c);
                nextTitleCase = false;
            }

            titleCase.append(c);
        }

        return titleCase.toString();
    }

    public int compareTo(Element other) {
        return this.name.compareTo(other.name);
    }

    private class Recipe {
        String element1;
        String element2;

        public Recipe(String one, String two) {
            this.element1 = one;
            this.element2 = two;
        }

        public boolean TestRecipe(String e1, String e2) {
            return e1.toLowerCase().equals(this.element1.toLowerCase()) && e2.toLowerCase().equals(this.element2.toLowerCase()) || e1.toLowerCase().equals(this.element2.toLowerCase()) && e2.toLowerCase().equals(this.element1.toLowerCase());
        }

        public String toString() {
            return this.element1 + "+" + this.element2;
        }

        public void DrawRecipe(IOHandler io) {
            io.SetTextColor(ElementCollection.GetElementColor(this.element1));
            io.Write(Element.toTitleCase(this.element1));
            io.SetTextColor(TextColor.WHITE);
            io.Write(" + ");
            io.SetTextColor(ElementCollection.GetElementColor(this.element2));
            io.WriteLine(Element.toTitleCase(this.element2));
        }
    }
}
